// pages/me/index/index.js
import { Api } from "../../../utils/api.js"
import { Http } from '../../../utils/http.js'
import { AccurateMath } from '../../../utils/accurateMath.js'
import { TimeToDate } from '../../../utils/timeToDate.js'
let http = new Http()
let accurateMath = new AccurateMath();
let timeToDate = new TimeToDate()

Page({

  /**
   * 页面的初始数据
   */
  data: {
    select: 1,
    orderStatus: [
      {
        title: '待付款',
        icon: '/images/icon/orderUnPay.png',
        type: 1,
        number: 0
      },
      {
        title: '待发货',
        icon: '/images/icon/not_shipped.png',
        type: 2,
        number: 0
      },
      {
        title: '配送中',
        icon: '/images/icon/orderDistribution.png',
        type: 3,
        number: 0
      },
      {
        title: '已完成',
        icon: '/images/icon/orderFinish.png',
        type: 4,
        number: 0
      }
    ],
    navList: [
      {
        title: '优惠券',
        icon: '/images/icon/coupon.png',
        url: '/pages/me/coupon/coupon?',
        width: 26,
        height: 22
      },
      {
        title: '收货地址',
        icon: '/images/icon/address.png',
        url: 'openAddress',
        width: 22,
        height: 28
      },
      {
        title: '售后服务',
        icon: '/images/icon/service.png',
        url: '/pages/me/service/service?',
        width: 26,
        height: 30
      },
      {
        title: '帮助中心',
        icon: '/images/icon/help.png',
        url: '/pages/system/help/help',
        width: 28,
        height: 28
      },
      // {
      //   title: '产品建议',
      //   icon: '/images/icon/advice.png',
      //   url: '/pages/system/advice/advice',
      //   width: 28,
      //   height: 28
      // }
    ],
    user: {
      avatar: '',
      nickname: '',
      level: 0,
      money: 0.00,
      withdraw: 0.00,
      isProxy: 0
    },
    userLogin: 0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getUserInfo()
    this.countOrderNum()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  /**
   * 自定义方法
   */
  selectNav: function (e) {
    var select = e.currentTarget.dataset.select
    this.setData({
      select: select
    })
  },

  goPage: function (e) {
    var url = e.currentTarget.dataset.url
    if (url == 'openAddress') {
      var that = this
      wx.chooseAddress({
        success: function (res) {
        },
        fail: function (res) {
          if (res.errMsg == 'chooseAddress:fail auth deny') {
            wx.showModal({
              title: '提示',
              content: '请到设置中允许微信打开',
              confirmText: '去设置',
              success: function(res) {
                if(res.confirm) {
                  wx.openSetting({
                    success: function (res) {
                      
                    },
                    fail: function (res) {
                      wx.showToast({
                        title: '打开设置失败，请手动打开',
                        icon: 'none'
                      })
                    }
                  })
                }
              }
            })
          }
        }
      })
    } else {
      wx.navigateTo({
        url: url,
      })
    }
  },

  getUserInfo: function() {
    var userToken = getApp().globalData.userToken
    var that = this
    if (userToken.length > 0) {
      http.sendHttp(Api.get_user_info, {
        data: {
          
        },
        success: function (res) {
          var user = res.data.obj
          user.income = accurateMath.accAdd(user.money, user.withdraw)
          if (user.isProxy == 1) {
            user.tmAudit = timeToDate.formatTime(user.tmAudit / 1000, 'Y.M.D')
          }else{
            that.setData({
              select: 1
            })
          }
          that.setData({
            user: user,
            userLogin: 1
          })
        }
      })
    }
  },

  goLogin: function () {
    wx.navigateTo({
      url: '/pages/system/login/login',
    })
  },

  goOrder: function (e) {
    var currentTab = e.currentTarget.dataset.currentTab
    wx.navigateTo({
      url: '/pages/me/order/order?currentTab=' + currentTab,
    })
  },

  goIncome: function () {
    wx.navigateTo({
      url: '/pages/me/income/income?user=' + JSON.stringify(this.data.user),
    })
  },

  showToast: function () {
    wx.showToast({
      title: '功能正在开发中 ^_^',
      icon: 'none'
    })
  },

  countOrderNum: function () {
    var that = this
    var orderStatus = this.data.orderStatus
    http.sendHttp(Api.count_order_num, {
      data: { },
      success: function (res) {
        orderStatus.forEach((state, index) => {
          that.setData({
            [`orderStatus[${index}].number`]: 0
          })
          res.data.obj.orders.forEach(item => {
            if (item.state == state.type) {
              that.setData({
                [`orderStatus[${index}].number`]: item.number
              })
            }
          })
        })
      }
    })
  }
})